<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

<?

echo	"
		<br/><div class=\"alert alert-info alert-dismissable\">
			<i class=\"fa fa-info\"></i>
			<button aria-hidden=\"true\" data-dismiss=\"alert\" class=\"close\" type=\"button\">×</button>
		";
echo		"<div class=\"row\">";
foreach($getPageCategory as $rnev => $hnev) {
	echo		"<div class=\"col-xs-4\"><strong>".$rnev."</strong>: ".$hnev."</div>";
}
echo		"</div>";
echo	"</div>";
?>

<table id="privilegeTable" class="display table table-bordered">
	<thead>
		<tr>
			<th><?=$this->lang->line("INSTITUTE")?></th>
			<?
			foreach($getPageCategory as $rnev => $hnev) {
				echo		"<th class=\"text-center\">".$rnev."</th>";
			}
			?>
		</tr>
	</thead>
	<tbody>
		<?php
		foreach($getInstituteList as $ins) {
			echo		"<tr>";
			echo			"<td class=\"vm\">".$ins["insName"]." (".$ins["cityName"].")</td>";
			foreach($getPageCategory as $szo_rnev => $szo_hnev) {
				echo			"<td class=\"vm text-center\">";
				if(isset($getUsr2ent["PAGE_CAT_".$szo_rnev]["u2eIds"][$ins["insId"]])) {
					echo				"<input name=\"".$szo_rnev."_".$ins["insId"]."\" data-category=\"".$szo_rnev."\" data-ins=\"".$ins["insId"]."\" type=\"checkbox\" checked>";
				} elseif(isset($this->getUsr2ent["PAGE_CAT_".$szo_rnev]["u2eIds"][$ins["insId"]]) or Common::isSuperAdmin()) {
					echo				"<input name=\"".$szo_rnev."_".$ins["insId"]."\" data-category=\"".$szo_rnev."\" data-ins=\"".$ins["insId"]."\" type=\"checkbox\">";
				}
				echo			"</td>";
			}
			echo		"</tr>";
		}
		?>
	</tbody>
</table>

<script>
	$(document).ready( function () {

		var privilegeTable = $('#privilegeTable').DataTable({
			"language": {
				"url": "<?=MEDIA?>admin_lte/DataTables-1.10.3/localization/en.lang"
			},
			ordering: false,
			paging: false,
			searching: false,
			"autoWidth": false,
			"order": [[ 0, 'asc' ]],
			stateSave: true
		});

		privilegeTable.on('draw.dt', function () {
			$('#privilegeTable').removeClass('hidden');

			$('input[data-category]').unbind('change').on('change', function() {
				$this = $(this);
				$.ajax({
					type: "POST",
					cache: false,
					data: { usrId: '<?=$this->input->post("ID")?>', ENTITY: $this.data('category'), insId: $this.data('ins'), checked: $this.is(':checked') },
					url: '<?=site_url("admin_lte/usr2ent_insert_update")?>',
					error: function (error) {}
				});
			});

		});

	});

</script>